//
//  GPCustomAnimationImageView.m
//  ios_yuece
//
//  Created by GuoPeng on 16/3/10.
//  Copyright © 2016年 GuoPeng. All rights reserved.
//

#import "GPCustomAnimationImageView.h"
#import <POP/POP.h>

typedef struct {
    CGFloat progress;
    CGFloat toValue;
    CGFloat currentValue;
} AnimationInfo;

@interface GPCustomAnimationImageView ()
{
    
}

@property (nonatomic, assign) CGFloat toValue;


@end

@implementation GPCustomAnimationImageView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setUp];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUp];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self setUp];
    }
    return self;
}

- (void)setUp
{
    UITapGestureRecognizer *tapImageGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doSelectCellImage:)];
    self.userInteractionEnabled = YES;
    [self addGestureRecognizer:tapImageGesture];
}

- (void)doSelectCellImage:(UIGestureRecognizer *)sender
{
//    UIImageView *imageView = (UIImageView *)sender.view;
    if (sender.state == UIGestureRecognizerStateEnded) {
        
    }
    AnimationInfo animationInfo = [self animationInfoForLayer:self.layer];
    BOOL hasAnimations = self.layer.pop_animationKeys.count;
    if (hasAnimations && animationInfo.progress < 0.98) {
        [self pauseAllAnimations:NO forLayer:self.layer];
        return;
    }
    
    [self.layer pop_removeAllAnimations];
    if (/*animationInfo.toValue >= 1 &&*/ self.toValue >1) {
        [self scaleDownView:self];
        return;
    }
    [self scaleUpView:self];
}

- (void)pauseAllAnimations:(BOOL)pause forLayer:(CALayer *)layer
{
    for (NSString *key in layer.pop_animationKeys) {
        POPAnimation *animation = [layer pop_animationForKey:key];
        [animation setPaused:pause];
    }
}

- (void)scaleDownView:(UIView *)view
{
    POPSpringAnimation *scaleAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerScaleXY];
    scaleAnimation.toValue = [NSValue valueWithCGSize:CGSizeMake(1, 1)];
    self.toValue = 1;
    scaleAnimation.springBounciness = 20.f;
    [view.layer pop_addAnimation:scaleAnimation forKey:@"scaleAnimation"];
}

- (AnimationInfo)animationInfoForLayer:(CALayer *)layer
{
    POPSpringAnimation *animation = [layer pop_animationForKey:@"scaleAnimation"];
    CGPoint toValue = [animation.toValue CGPointValue];
    CGPoint currentValue = [[animation valueForKey:@"currentValue"] CGPointValue];
    
    CGFloat min = MIN(toValue.x, currentValue.x);
    CGFloat max = MAX(toValue.x, currentValue.x);
    
    AnimationInfo info;
    info.toValue = toValue.x;
    info.currentValue = currentValue.x;
    info.progress = min / max;
    return info;
}

-(void)scaleUpView:(UIView *)view
{
    POPSpringAnimation *positionAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerPosition];
    positionAnimation.toValue = [NSValue valueWithCGPoint:self.center];
    [view.layer pop_addAnimation:positionAnimation forKey:@"layerPositionAnimation"];
    
    POPSpringAnimation *scaleAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerScaleXY];
    scaleAnimation.toValue = [NSValue valueWithCGSize:CGSizeMake(1.4, 1.4)];
    self.toValue = 1.4;
    scaleAnimation.springBounciness = 20.f;
    [view.layer pop_addAnimation:scaleAnimation forKey:@"scaleAnimation"];
}





@end
